package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IRegionService regionService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategyCommendService strategyCommendService;

    // 攻略收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo){
        // 判断当前用户是否登陆
        if(userInfo == null){
            // 没有登陆
            AjaxResult ajax = new AjaxResult(false, "请先登陆");
            ajax.setCode(102);
            return ajax;
        }


        // 约定: 如果ret true表示收藏, 如果ret false取消收藏
        boolean ret = strategyStatisRedisService.favor(sid, userInfo.getId());
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(sid);

        // 国内国外攻略推荐加分
        strategyStatisRedisService.addCommendScore(sid, ret?1:1);

        return new AjaxResult(ret).addData(vo);
    }

    @RequestMapping("detail")
    public String detail(Model model, Long id){
        //detail
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        detail.setStrategyContent(content);

        model.addAttribute("detail",detail );

        // 阅读数 + 1
        strategyStatisRedisService.addViewnum(id, 1);

        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(id);

        model.addAttribute("vo", vo);

        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String List(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());

        Destination dest = toasts.remove(toasts.size() - 1);
        // toasts
        model.addAttribute("toasts", toasts);
        // dest
        model.addAttribute("dest", dest);

        //tags
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags", tags);


        PageInfo page = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", page);

        return "strategy/list";
    }

    // 攻略评论
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setCreateTime(new Date());// 时间

        strategyCommentService.save(comment);

        // 评论数 + 1
        strategyStatisRedisService.addReplynum(comment.getDetailId(), 1);

        // 热门攻略排行 + 分
        strategyStatisRedisService.addHotScore(comment.getDetailId(),1);

        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(comment.getDetailId());
        return AjaxResult.SUCCESS.addData(vo.getReplynum());
    }

    // 评论的列表
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo")StrategyCommentQuery qo){
        // 此处的page不是mybatis那个pageInfo, 而是 spring-data-jpa 里面分页对象 Page
        model.addAttribute("page", strategyCommentService.query(qo));
        return "strategy/commentTpl";
    }

    // 评论点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(Long sid, @UserParam UserInfo userInfo){
        // 判断当前用户是否登陆
        if(userInfo == null){
            // 没有登陆
            AjaxResult ajax = new AjaxResult(false, "请先登陆");
            ajax.setCode(102);
            return ajax;
        }
        // 约定: 如果ret true表示顶成功, 如果ret false 已经顶过
        boolean ret = strategyStatisRedisService.thumbup(sid, userInfo.getId());
        if(ret){
            // 国内国外攻略推荐加分
            strategyStatisRedisService.addCommendScore(sid, 1);
        }
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(sid);

        return new AjaxResult(ret).addData(vo);
    }

    @RequestMapping("")
    public String index(Model model){
        // commends
        model.addAttribute("commends", strategyCommendService.getTop5());

        // 页面实现:热门推荐排行
        // hotCds
        List<StrategyStatisVO> hotCds = strategyStatisRedisService.queryHotCommendTop10();
        model.addAttribute("hotCds", hotCds);

        // 国内攻略推荐排行
        List<StrategyStatisVO> unabroadCds = strategyStatisRedisService.queryChinaCommendTop10();
        model.addAttribute("unabroadCds", unabroadCds);
        // 国外攻略推荐排行
        List<StrategyStatisVO> abroadCds = strategyStatisRedisService.queryAbroadCommendTop10();
        model.addAttribute("abroadCds", abroadCds);

        return "strategy/index";
    }
}
